﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EWDT_CDS_Hub.App_Code;

namespace EWDT_CDS_Hub
{
    public partial class RequestControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String id = Session["StudentID"].ToString();
            //Subject(s) that is to be taken this semester (can be changed/dropped)
            List<Subject> subjectCurrentList = Subject.RetrieveCurrentSubject(id);
            //Subject(s) that are not taken or to be taken 
            List<Subject> subjectChooseableList = Subject.RetrieveChooseableSubject(id);

            //Get subjects that are chosen in all of the student's appeal(s)
            //Subjects that are in a submitted appeal will not be available in another appeal.
            List<Appeal> allAppealsList = Appeal.RetrieveStudentAppeals(id);
            List<Subject> appealedSubjectList = new List<Subject>();
            List<Choice> choiceList = new List<Choice>();
            Choice cTemp = new Choice();
            Subject sTemp = new Subject();

            foreach (Appeal a in allAppealsList)
            {
                choiceList = Appeal.RetrieveChoiceList(a.Appeal_id);
                foreach (Choice c in choiceList)
                {
                    sTemp = Subject.RetrieveSubject(c.Subject_id);
                    appealedSubjectList.Add(sTemp);
                }
            }

            //Filter subjectChooseableList
            for (int i = subjectChooseableList.Count -1; i >= 0; i--)
            {
                Subject temp = subjectChooseableList[i];
                foreach (Subject s in appealedSubjectList)
                {
                    if (temp.Subject_id == s.Subject_id)
                        subjectChooseableList.RemoveAt(i);
                }
            }

            //If student do not have any CDS to drop or change, the 2 options will not be available.
            if (rblOptions.Items.Count == 0)
            {
                ListItem item = new ListItem("Add CDS", "ADD");
                rblOptions.Items.Add(item);

                if (subjectCurrentList.Count != 0)
                {
                    ListItem item2 = new ListItem("Drop CDS", "DROP");
                    ListItem item3 = new ListItem("Change CDS", "CHANGE");

                    rblOptions.Items.Add(item2);
                    rblOptions.Items.Add(item3);
                }
            }

            //Databind rblDrop
            rblDrop.DataSource = subjectCurrentList;
            rblDrop.DataTextField = "Subject_name";
            rblDrop.DataValueField = "Subject_id";
            rblDrop.DataBind();

            //Databind lbxCDSList
            lbxCDSList.DataSource = subjectChooseableList;
            lbxCDSList.DataTextField = "Subject_name";
            lbxCDSList.DataValueField = "Subject_id";
            lbxCDSList.DataBind();
        }

        protected void btnMove_Click(object sender, EventArgs e)
        {
            ListItem item = new ListItem();                      

            //Move to left
            if (btnMove.Text == ">>")
            {
                if (lbxCDSList.SelectedItem == null)
                    return;

                item.Value = lbxCDSList.SelectedItem.Value;
                item.Text = lbxCDSList.SelectedItem.Text;

                lbxChoice.Items.Add(item);
                lbxCDSList.Items.Remove(item);
            }

            //Move to right
            if (btnMove.Text == "<<")
            {
                if (lbxChoice.SelectedItem == null)
                    return;

                item.Value = lbxChoice.SelectedItem.Value;
                item.Text = lbxChoice.SelectedItem.Text;

                lbxCDSList.Items.Add(item);
                lbxChoice.Items.Remove(item);
            }
        }

        protected void rblOptions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxCDSList.SelectedItem != null)
                btnMove.Text = ">>";
            else if (lbxChoice.SelectedItem != null)
                btnMove.Text = "<<";

            pnlStep2A.Visible = false;
            pnlStep2B.Visible = false;

            String index = rblOptions.SelectedValue;

            if (index == "ADD")
            {
                pnlStep2B.Visible = true;
                lblAdd.Text = "STEP 2: Select List of Choices (Arrange choices in order of priority)";
            }

            if (index == "DROP")
            {
                pnlStep2A.Visible = true;
            }

            if (index == "CHANGE")
            {
                pnlStep2A.Visible = true;
                pnlStep2B.Visible = true;
                lblAdd.Text = "STEP 3: Select List of Choices (Arrange choices in order of priority)";
            }

        }        
    }
}